< previous page page_556 next page >

Page 556
Database Errors with Microsoft Data Access Objects
If you use Microsoft data access objects instead of the Visual Basic data control, you need to add error-handling routines to your project. For example, if you want to create a Dynaset using Visual Basic code, you need to trap for any error that might occur along the way.
Add the code in Listing 14.12 to the Form_Load event of frmData. This code opens the database and creates a Dynaset to stuff into the data control that already exists on the form.
Type.gifListing 14.12. Adding code to the Form_Load event.
Private Sub Form_Load()
    '
    ' create recordset using DAO
    '
    On Error GoTo LocalErr
    '
    Dim ws As Workspace
    Dim db As Database
    Dim rs As Recordset
    Dim strSQL As String
    '
    strSQL = "SELECT * FROM Table2"
    Set ws = DBEngine.Workspaces(0)
    Set db = ws.OpenDatabase(App.Path & "\..\..\Data\Errors\ErrorDB.mdb")
    Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    Exit Sub
    '
LocalErr:
    MsgBox "<" & CStr(Errors(0).Number) & "> " & Errors(0).Description,
arrow.gifvbCritical, "Form_Load Error"
    Unload Me
    '
End Sub
The code in Listing 14.12 establishes some variables and then opens the database and creates a new Dynaset from a data table called Table2.
Note.gif
Notice that instead of the Visual Basic Err object, the DAO Errors collection is used to retrieve the most recent database error. The Errors collection is only available if you loaded the Microsoft DAO library using the Project | References option from the main Visual Basic 5 menu.

 
< previous page page_556 next page >

If you like this book, buy it!